package com.hsource.doctorcenter.controller;

import com.hsource.common.utils.CommonCallBackResp;
import com.hsource.common.utils.CommonResp;
import com.hsource.common.utils.resultUtils.controller.BaseController;
import com.hsource.doctorcenter.domain.doctor.DoctorApi;
import com.hsource.doctorcenter.manager.AsyncManager;
import com.hsource.doctorcenter.manager.factory.AsyncFactory;
import com.hsource.doctorcenter.service.clinic.IClinicService;
import com.hsource.doctorcenter.service.doctor.IDoctorService;
import com.hsource.doctorcenter.util.ServletUtils;
import com.hsource.doctorcenter.util.StaticValue;
import com.hsource.doctorcenter.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yangYang
 * @date  2020/9/21 15:22:22
 * @version 1.0
 * @describe 医生信息控制层
 */
@Controller
@RequestMapping("/doctors")
public class DoctorsController extends BaseController {

    @Autowired
    private IClinicService clinicService;
    @Autowired
    private IDoctorService doctorService;





    /**
     * 获取医生时长价格|可预约时间
     * @param userId
     * @param doctorId
     * @return
     * @throws ParseException
     * @throws NoSuchAlgorithmException
     */
    @PostMapping("/getDoctorPhoneInfo")
    @ResponseBody
    public CommonResp getDoctorPhoneInfo(String userId ,String doctorId)throws ParseException, NoSuchAlgorithmException {
        Map<String, Object> doctorsMap = doctorService.getDoctorPhoneInfo(userId, doctorId);
        if(doctorsMap.get("error").toString().equals("0")) {
            return success(doctorsMap);
        }else{
            return failed(doctorsMap.get("error_msg").toString());
        }
    }
    /**
     * 获取医生详情
     * @param userId 用户id
     * @param doctorId 医生id
     * @return
     */
    @PostMapping("/detail")
    @ResponseBody
    public CommonResp doctorsDetailed(String userId,String doctorId) throws ParseException, NoSuchAlgorithmException {
        Map<String, Object> doctorsMap = new HashMap<>();
        doctorsMap.put("doctorInfo", doctorService.getDoctorsDetailed(userId, doctorId));
        return success(doctorsMap);
    }
    /**
     * 找医生接口
     * @param userId 用户id
     * @param famousDoctor 是否筛选名医
     * @param startNum 开始数
     * @param count 每次取的医生数
     * @param province 省份
     * @param city 城市
     * @param serviceType 服务类型
     * @return
     * @throws ParseException
     * @throws NoSuchAlgorithmException
     */
    @PostMapping("/getDoctorsList")
    @ResponseBody
    public CommonResp getDoctorsList(String userId,Integer famousDoctor,Integer startNum,Integer count,String province,String city,String serviceType,String clinicNo,String sortType) throws ParseException, NoSuchAlgorithmException {
        //生成校验参数集合
        Map<String, Object> doctorsMap = doctorService.getDoctorsList(userId
                , StringUtils.isNotNull(famousDoctor) ? famousDoctor : 0
                , startNum
                , StringUtils.isNotNull(count) ? count : 20
                , province
                , city
                , serviceType
                , StringUtils.isNotBlank(clinicNo) ? clinicNo : "1"
                , sortType
        );
        if(doctorsMap.get("error").toString().equals("0")) {
            doctorsMap.remove("error");
            doctorsMap.remove("error_msg");

            return success(doctorsMap);
        }else{
            return failed(doctorsMap.get("error_msg").toString());
        }
    }


    /**
     * 推荐医生|选医生列表
     * @param userId 用户id
     * @param ask 首次提问内容文本
     * @return
     * @throws ParseException
     * @throws NoSuchAlgorithmException
     */
    @PostMapping("/getRecommendedDoctors")
    @ResponseBody
    public CommonResp getRecommendedDoctors(String userId,String ask) throws ParseException, NoSuchAlgorithmException {
        //生成校验参数集合
        Map<String, Object> doctorsMap = doctorService.recommendedDoctors(userId, ask);
        if(doctorsMap.get("error").toString().equals("0")) {
            //单独获取系统指派医生信息
            List<DoctorApi> doctorApis = doctorService.selectDoctorListByDoctorIds("0");
            doctorApis.stream().forEach(da->{
                //指派医生根据配置文件重写价格
                da.setPrice(StringUtils.isNoneBlank(StaticValue.SYSTEM_ASSIGNMENT_DOCTOR_PRICE)?Long.parseLong(StaticValue.SYSTEM_ASSIGNMENT_DOCTOR_PRICE):da.getPrice());
            });
            List<DoctorApi> daList = (List<DoctorApi>) doctorsMap.get("doctors");
            doctorApis.addAll(daList);
            doctorsMap.put("doctors",doctorApis);
            doctorsMap.remove("error");
            doctorsMap.remove("error_msg");
            return success(doctorsMap);
        }else{
            return failed(doctorsMap.get("error_msg").toString());
        }
    }

    /**
     * 搜索医生接口
     * @param userId 用户id
     * @param queryText 搜索词
     * @param province 省份
     * @param city 城市
     * @return
     * @throws ParseException
     * @throws NoSuchAlgorithmException
     */
    @PostMapping("/searchDoctors")
    @ResponseBody
    public CommonResp searchDoctors(String userId,String queryText,String province,String city,Integer page) throws ParseException, NoSuchAlgorithmException {
        //生成校验参数集合
        Map<String, Object> doctorsMap = doctorService.searchDoctors(userId
                , queryText
                , StringUtils.isNotNull(page)?page:0
                , province
                , city
        );
        if(doctorsMap.get("error").toString().equals("0")) {
            doctorsMap.remove("error");
            doctorsMap.remove("error_msg");

            return success(doctorsMap);
        }else{
            return failed(doctorsMap.get("error_msg").toString());
        }
    }

}
